import {callFileCommand} from '../../src/cmd'
import {expect} from 'chai'
import path from 'path'
import fs from 'fs'

describe('test file command', () => {
    it('missing command file path should throw error', () => {
        process.env.FLOW_PATH = ''

        try {
            callFileCommand('PATH', '/path/to/add')
            expect(true).to.be.false
        } catch (error) {
            expect(true).to.be.ok
        }
    })

    it('missing command file should throw error', () => {
        process.env.FLOW_PATH = path.join(__dirname, 'files/temp-file1')

        try {
            callFileCommand('PATH', '/path/to/add')
            expect(true).to.be.false
        } catch (error) {
            expect(true).to.be.ok
        }
    })

    it('call file command should append command to file', () => {
        const cmdFilePath: string = path.join(__dirname, 'files/cmd-file.txt')
        process.env.FLOW_CMD1 = cmdFilePath

        fs.writeFileSync(cmdFilePath, 'beginning-line\n', 'utf-8')

        callFileCommand('CMD1', 'cmd1-message1')
        callFileCommand('CMD1', 'cmd1-message2')
        const cmdFileContent = fs.readFileSync(cmdFilePath, 'utf8')
        expect(cmdFileContent).to.equal(
            `beginning-line\ncmd1-message1\ncmd1-message2\n`
        )
    })
})
